<?php

namespace Frieza\Modules\Position\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Biz\pockets\excel\DownExcel;
use Biz\service\bizTools\QueryWords;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class QueryWordsController extends Controller
{

    public function index(Request $request, Content $content)
    {

        $params = $request->all();

        $keyWords = $params['keywords'] ?? '';

        $startDay = date('Ymd', strtotime("-7 days", time()));

        $endDay = date('Ymd', time());

        $form = new Form();

        $form->text('keywords', '种子词')->default($keyWords)->help('词包内主要关键词，如厨房、卫生间等，词包内容均为种子词的拓展');

        $form->method('GET');

        $form->action('/admin/position/queryWords/index');

        $form->disableReset();

        $lists = [];

        if (!empty($keyWords)) {
            $lists = QueryWords::query($startDay, $endDay, $keyWords);
        }

        $top = '<div class="box" >' . '<div style="font-weight: 700;padding: 8px 8px;font-size: 18px;">词包条件</div>' . $form->render() . '</div>';

        $content->row($top);

        $queryWords = empty($lists) ? [] : array_column($lists, 'query_words');

        $count = count($lists);

        $headers = ['搜索词', '搜索量'];

        $table = new Table($headers, $lists);

        $queryWordsStr = implode(',', $queryWords);

        $downloadUrl = url('/admin/position/queryWords/export?keywords=' . urlencode($keyWords));

        $downloadButton = '<div class="btn-group download" style="padding: 8px 8px;">
    <a class="btn btn-sm btn-twitter" target="_blank" title="下载词包" href="' . $downloadUrl . '"><i class="fa fa-download"></i><span class="hidden-xs">下载</span></a></div>';

        $dataShow = '<div class="box">';
        $dataShow .= '<div style="font-weight: 700;padding: 8px 8px;font-size: 18px;">查询结果</div>';
        if($count) {
            $dataShow .= '<div style="padding: 8px 8px;">词包：' . $queryWordsStr . '</div>';
            $dataShow .= '<div style="padding: 8px 8px;">词包数据(<span style="color: #737373">词包内的搜索词在过去7天内的总搜索量</span>)</div>';
            $dataShow .= $downloadButton;
            $dataShow .= $table->render();
        }else{
            $dataShow .= '<div style="padding-bottom: 50px;"></div>';
        }
        $dataShow .= '</div>';

        $content->row($dataShow);

        return $content
            ->header('词包查询')
            ->description(' ')
            ->breadcrumb(['text' => '词包查询', 'url' => '/position/queryWords/index']);
    }


    public function export(Request $request)
    {
        $params = $request->all();

        $keyWords = $params['keywords'] ?? null;

        $currentTime = time();

        $startDay = date('Ymd', strtotime("-7 days", $currentTime));

        $endDay = date('Ymd', $currentTime);

        $lists = [];

        if (!empty($keyWords)) {
            $lists = QueryWords::query($startDay, $endDay, $keyWords);
        }

        if (empty($lists)) {
            $success = new MessageBag([
                'title' => '暂无词包数据导出'
            ]);

            return back()->with(compact('success'));
        }

        $header = ['搜索词', '搜索量'];

        $yesterDay = date('Ymd', strtotime("-1 days", $currentTime));

        $excel_obj = new DownExcel();
        $excel_obj->setFileName('词包 - ' . date('Ymd'));
        $excel_obj->setSheetTitle($startDay . '_' . $yesterDay);

        $widths = [
            'A' => 20,
            'B' => 22,
        ];

        $excel_obj->setWidth($widths);
        $excel_obj->setDefaultStyleAlignment();

        $excel_obj->create($lists, $header);
    }


}